package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;



/**
 * 修改用户的账户编号
 * lch
 */
public class ChangeUserAccount implements ServerInterface {
    private Logger log = LogManager.getLogger(ChangeUserAccount.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            // 入参转换
            UserBean bean = JsonUtils.objectToPojo(req.getData(), UserBean.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            if(StringUtils.isBlank(bean.getUserNo()))
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "用户编号不能为空", Void.class);
            if(StringUtils.isBlank(bean.getCtmNo()))
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "客户编号不能为空", Void.class);
            if(StringUtils.isBlank(bean.getAccountNo()))
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "账户编号不能为空", Void.class);
            if(StringUtils.isBlank(bean.getNewAccountNo()))
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "新的账户编号不能为空", Void.class);
            int count = userInfoMapper.updateUserAccountNo(bean);
            if(count <= 0)
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "修改失败", Void.class);
            // 删除旧的三户关系
            userInfoMapper.userRelationHstDelete(bean.getCtmNo(), bean.getUserNo(), bean.getAccountNo());
            // 添加新的三户关系
            userInfoMapper.userRelationHstAdd(Tools.getObjectId(), bean.getCtmNo(), bean.getUserNo(), bean.getNewAccountNo());
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", void.class);
        }catch(Exception e){
            log.error("修改用户账户编号失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "修改失败", Void.class);
        }
    }
}
